package com.prs.crm.action.cqunicom;

import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.cqunicom.BusinessRemind;
import com.prs.crm.domain.cqunicom.LogSerch;
import com.prs.crm.domain.cqunicom.NewsSendContent;
import com.prs.crm.domain.cqunicom.NewsSendPerson;
import com.prs.crm.domain.cqunicom.Services;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Role;
import com.prs.crm.service.cqunicom.BusinessRemindService;
import com.prs.crm.service.cqunicom.LogSerchService;
import com.prs.crm.service.cqunicom.NewsSendContentService;
import com.prs.crm.service.cqunicom.NewsSendPersonService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.hr.RoleService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-3-3 上午10:21:20 简单说明 公布发送
 */

public class NewsSendContentAction extends BaseAction {

	private static final long serialVersionUID = 1L;
    private String rowdata;
	private List<BusinessRemind> remindList;
	private BusinessRemindService businessRemindService;
	private List<NewsSendPerson> notes;
	private PaginationSupport pagination;
	private LogSerchService logSerchService;
	private NewsSendContent newsSendContent;

	private NewsSendPerson newsSendPerson;

	private NewsSendPersonService newsSendPersonService;

	private NewsSendContentService newsSendContentService;
	
	private Role role;

	private RoleService roleService;
	
	private Person person;
	
	private PersonService personService;

	private String sendStyle;
	
	private String sendIds;
	
	private Integer sendTeams;
    private String[] names= {"专业坐席","班长","订单管理员"}; 
	
	private String userStyle;
	
	@Action("editSendNews")
	public String editSendNews() {
		return SUCCESS;
	}
	
	@Action("lookNotices")
	public String lookNotices() {
		NewsSendPerson nsp=this.getNewsSendPersonService().get(newsSendPerson.getId());
		nsp.setReadMark("是");
		this.getNewsSendPersonService().submit(nsp);		
		return SUCCESS;
	}
	@Action("receivNotice")
	public String receivNotice() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NewsSendPerson.class);
		criteria.add(Restrictions.eq("persons",AppContext.getAccountContext().getUser()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewsSendContentService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	
	@Action("ReadreceiveNotice")
	public String ReadreceiveNotice() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NewsSendPerson.class);
		criteria.add(Restrictions.eq("persons",AppContext.getAccountContext().getUser()));
		criteria.add(Restrictions.eq("readMark","否"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewsSendContentService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}

	@Action("selectNoticeRole")
	public String selectNoticeRole() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Role.class);
		  if("超级管理员".equals(AppContext.getAccountContext().getUser().getUserStyle())){

		    }
		    else if ("班长".equals(AppContext.getAccountContext().getUser().getUserStyle())) {
				criteria.add(Restrictions.eq("name", "专业坐席"));
			}	
			else if("订单管理员".equals(AppContext.getAccountContext().getUser().getUserStyle())){
				criteria.add(Restrictions.or(Restrictions.eq("name", "专业坐席"),Restrictions.eq("name", "班长")));
				
			}
			else if("普通管理员".equals(AppContext.getAccountContext().getUser().getUserStyle())){
				
				criteria.add(Restrictions.in("name",names));
				
			}

		criteria.createAlias("type", "ty");
		criteria.add(Restrictions.eq("ty.name", "114联通"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getRoleService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	/**
	 * 历史消息列表
	 * 
	 * @return
	 */
	@Action("selectHistoryNotice")
	public String selectHistoryNotice() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NewsSendContent.class);
		// criteria.add(Restrictions.not(Exprission.eq()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewsSendContentService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	
	@Action("selectPersons")
	public String selectPersons() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Person.class);
		 if("超级管理员".equals(AppContext.getAccountContext().getUser().getUserStyle())){

		    }
		 else if ("班长".equals(AppContext.getAccountContext().getUser().getUserStyle())) {
				criteria.add(Restrictions.eq("userStyle", "专业坐席"));
			}	
		 
		 else if("订单管理员".equals(AppContext.getAccountContext().getUser().getUserStyle())){
				criteria.add(Restrictions.or(Restrictions.eq("userStyle", "专业坐席"),Restrictions.eq("userStyle", "班长")));
				
			}
		else if("普通管理员".equals(AppContext.getAccountContext().getUser().getUserStyle())){
				
				criteria.add(Restrictions.in("userStyle",names));
				
			}
		criteria.createAlias("role", "rr");
		criteria.createAlias("rr.type", "oo");
		criteria.add(Restrictions.eq("oo.name", "114联通"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPersonService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	@Clew(results = { "success", "none" })
	@Action(value = "saveSendNotices", results = {
			@Result(name = "success", type = "redirect", location = "editSendNews"),
			@Result(name = "none", type = "redirect", location = "editSendNews"),
			@Result(name = "error", type = "chain", location = "editSendNews") })
	public String saveSendNotices() {
		LogSerch  log=new LogSerch();
		log.setCandlePeron(AppContext.getAccountContext().currentUser().getUserName());
		log.setCandleContents("发布了公告:"+newsSendContent.getNewsTitle());
		this.getLogSerchService().save(log);
	 // System.out.println("sendStyle:"+sendStyle);
		if(sendStyle.equals("A")){
	//	System.out.println("进入A");	
		//按姓名
		//System.out.println("sendIds:"+sendIds);
		String ids[]=sendIds.split(",");
		for(String id:ids){
			if("".equals(id)|| id==null){
				
			}else{
				NewsSendPerson  np=new NewsSendPerson();
				//System.out.println("id:"+id);
				//System.out.println("存入ID："+this.getPersonService().get(Integer.parseInt(id)).getId());
				np.setPersons(this.getPersonService().get(Integer.parseInt(id)));
				np.setPersonn(AppContext.getAccountContext().currentUser());
				np.setReadMark("否");
				np.setSendTitle(newsSendContent.getNewsTitle());
				np.setSendContent(newsSendContent.getNewsContents());
				np.setRecordTime(new Date());
				this.getNewsSendPersonService().save(np);
				this.getNewsSendContentService().save(newsSendContent);
			}
		}
			
		}else if(sendStyle.equals("B")){
		//按組別
			//System.out.println("进入b");	
		List<Person> listPerson=this.getPersonService().getNoticePerson(role);
		for(Person p:listPerson){
			NewsSendPerson  np=new NewsSendPerson();
			//System.out.println("存入ID："+p.getId());
			np.setPersons(p);
			np.setPersonn(AppContext.getAccountContext().currentUser());
			np.setReadMark("否");
			np.setSendTitle(newsSendContent.getNewsTitle());
			np.setSendContent(newsSendContent.getNewsContents());
			np.setRecordTime(new Date());
			this.getNewsSendPersonService().save(np);
			this.getNewsSendContentService().save(newsSendContent);	
		}
			
		}
		
		
		this.setPrompt("公告發送成功");
		return SUCCESS;
	}
 /**
  * 接收到的公告
  * @return
  */
	@Action("ownReceivNotice")
	public String ownReceivNotice() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NewsSendPerson.class);
		criteria.add(Restrictions.eq("persons",AppContext.getAccountContext().currentUser()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNewsSendContentService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	
	
	
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public NewsSendContent getNewsSendContent() {
		return newsSendContent;
	}

	public void setNewsSendContent(NewsSendContent newsSendContent) {
		this.newsSendContent = newsSendContent;
	}

	public NewsSendPerson getNewsSendPerson() {
		return newsSendPerson;
	}

	public void setNewsSendPerson(NewsSendPerson newsSendPerson) {
		this.newsSendPerson = newsSendPerson;
	}

	public NewsSendContentService getNewsSendContentService() {
		return newsSendContentService;
	}

	public void setNewsSendContentService(
			NewsSendContentService newsSendContentService) {
		this.newsSendContentService = newsSendContentService;
	}

	public String getSendStyle() {
		return sendStyle;
	}

	public void setSendStyle(String sendStyle) {
		this.sendStyle = sendStyle;
	}

	public NewsSendPersonService getNewsSendPersonService() {
		return newsSendPersonService;
	}

	public void setNewsSendPersonService(
			NewsSendPersonService newsSendPersonService) {
		this.newsSendPersonService = newsSendPersonService;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public String getSendIds() {
		return sendIds;
	}

	public void setSendIds(String sendIds) {
		this.sendIds = sendIds;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public Integer getSendTeams() {
		return sendTeams;
	}

	public void setSendTeams(Integer sendTeams) {
		this.sendTeams = sendTeams;
	}

	public LogSerchService getLogSerchService() {
		return logSerchService;
	}

	public void setLogSerchService(LogSerchService logSerchService) {
		this.logSerchService = logSerchService;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getRowdata() {
		return rowdata;
	}

	public void setRowdata(String rowdata) {
		this.rowdata = rowdata;
	}

	public List<BusinessRemind> getRemindList() {
		return remindList;
	}

	public void setRemindList(List<BusinessRemind> remindList) {
		this.remindList = remindList;
	}

	public BusinessRemindService getBusinessRemindService() {
		return businessRemindService;
	}

	public void setBusinessRemindService(BusinessRemindService businessRemindService) {
		this.businessRemindService = businessRemindService;
	}

	public List<NewsSendPerson> getNotes() {
		return notes;
	}

	public void setNotes(List<NewsSendPerson> notes) {
		this.notes = notes;
	}

	public String[] getNames() {
		return names;
	}

	public void setNames(String[] names) {
		this.names = names;
	}

	public String getUserStyle() {
		return userStyle;
	}

	public void setUserStyle(String userStyle) {
		this.userStyle = userStyle;
	}

}
